package ee.smkv.calc.loan.export;

import android.content.res.Resources;
import ee.smkv.calc.loan.model.Loan;
import ee.smkv.calc.loan.model.Payment;
import ee.smkv.calc.loan.R;
import ee.smkv.calc.loan.utils.ViewUtil;

import java.math.BigDecimal;

/**
 * @author samko
 */
public class HtmlScheduleCreator extends AbstractScheduleCreator {


    public HtmlScheduleCreator(Loan loan, Resources resources) {
        super(resources, loan);
    }

    protected String encode(int id) {
        return resources.getString(id);
    }

    public void appendHtmlScheduleTable(StringBuilder sb) {
        sb
                .append("<table><tr class=\"odd\"><th>")
                .append(encode(R.string.paymentNr))
                .append("</th><th>")
                .append(encode(R.string.paymentBalance))
                .append("</th><th>")
                .append(encode(R.string.paymentPrincipal))
                .append("</th><th>")
                .append(encode(R.string.paymentInterest))
                .append("</th><th>");
        if (hasDisposableCommission || hasMonthlyCommission) {
            sb
                    .append(encode(R.string.paymentCommission))
                    .append("</th><th>");
        }
        sb
                .append(encode(R.string.paymentTotal))
                .append("</th></tr>");
        int i = 0;

        if (hasDisposableCommission || hasDownPayment) {
            BigDecimal amount = BigDecimal.ZERO;
            sb.append("<tr class=\"")
                    .append(i++ % 2 == 0 ? "even" : "odd")
                    .append("\"><td>0</td><td>")
                    .append(ViewUtil.formatBigDecimal(loan.getAmount()))
                    .append("</td><td>")
                    .append(hasDownPayment ? ViewUtil.formatBigDecimal(loan.getDownPaymentPayment()) : "")
                    .append("</td><td></td><td>");
            if (hasDisposableCommission) {
                amount = amount.add(loan.getDisposableCommissionPayment());
                sb
                        .append(ViewUtil.formatBigDecimal(loan.getDisposableCommissionPayment()))
                        .append("</td><td>");
            }

            if (hasDownPayment) {
                amount = amount.add(loan.getDownPaymentPayment());
            }

            sb
                    .append(ViewUtil.formatBigDecimal(amount))
                    .append("</td></tr>");
        }

        for (Payment payment : loan.getPayments()) {
            sb.append("<tr class=\"")
                    .append(i++ % 2 == 0 ? "even" : "odd")
                    .append("\"><td>")
                    .append(payment.getNr().toString())
                    .append("</td><td>")
                    .append(ViewUtil.formatBigDecimal(payment.getBalance()))
                    .append("</td><td>")
                    .append(ViewUtil.formatBigDecimal(payment.getPrincipal()))
                    .append("</td><td>")
                    .append(ViewUtil.formatBigDecimal(payment.getInterest()))
                    .append("</td><td>");
            if (hasDisposableCommission || hasMonthlyCommission) {
                sb
                        .append(ViewUtil.formatBigDecimal(payment.getCommission()))
                        .append("</td><td>");
            }
            sb

                    .append(ViewUtil.formatBigDecimal(payment.getAmount()))
                    .append("</td></tr>");
        }
        sb
                .append("<tr><th>&nbsp;</th><th>&nbsp;</th><th>")
                .append(ViewUtil.formatBigDecimal(loan.getAmount()))
                .append("</th><th>")
                .append(ViewUtil.formatBigDecimal(loan.getTotalInterests()))
                .append("</th><th>");
        if (hasDisposableCommission || hasMonthlyCommission) {
            sb
                    .append(ViewUtil.formatBigDecimal(loan.getCommissionsTotal()))
                    .append("</th><th>");
        }
        sb
                .append(ViewUtil.formatBigDecimal(loan.getTotalAmount()))
                .append("</th></tr>")
                .append("</table>");
    }


    public void appendHtmlButtons(StringBuilder sb) {
        sb.append("<button id='closeBtn' onclick='window.schedule.finish();'>")
                .append("<span style='display:inline-block;width:100%;'>")
                .append("<img align=\"absmiddle\" src=''/> ")
                .append("<span style='display:inline-block;width:50%;text-align:center;'>")
                .append(encode(R.string.close))
                .append("</span>")
                .append("</span>")
                .append("</button>");
    }

    public void appendHtmlChart(StringBuilder sb, int width, int height) {
        sb
                .append("<canvas id=\"pie\" width=\"")
                .append(width)
                .append("\" height=\"")
                .append(height)
                .append("\"><br />Pie diagramm</canvas>")
                .append("<canvas id=\"line\" width=\"")
                .append(width)
                .append("\" height=\"")
                .append(height)
                .append("\">Loan amotrization</canvas>");
    }


}
